const config = require('../../../common/config');
const util = require('../../../utils/util');

var valueBak=[0,0];
var typeId=''; //当前选中的对象

Page({
    data: {
        isNext:true,
        value: [0, 0],
        bigTypes:[],
        bigTypeName:'',
        letTypes:[],
        letTypeName:''

    },
    onLoad:function (opt) {
        var that = this;
        wx.setNavigationBarTitle({
            title: '选择分类'
        })

        wx.showLoading({
            title: '数据加载..',
            icon: 'loading',
            duration: config.TIME_OUT,
            mask:true
        })

        wx.request({
            url: config.COMMPT_REQPATH,
            header: {
                "Content-Type": "application/x-www-form-urlencoded"
            },
            method:'POST',
            data: {
                accessToken: '',
                refreshToken: '',
                serviceName: 'dmService',
                methodName: 'selectProductsByParentIdExceptIds',
                reqData:JSON.stringify({
                    parentId:'1',
                    exceptId:['198']
                })
            },
            success: function (res) {
                var resultData = res.data;
                var products = resultData.data.products;
                if(products!= null && products.length>0){
                    that.setData({
                        bigTypes:products,
                        bigTypeName : products[0].productTypeName
                    })
                    that.queryLetProductTypes(products[0].productTypeId,[]);
                }

            },
            complete:function () {
                wx.hideLoading();
            }
        })


    },
    showNext:function (e) {
        var that  = this;
        wx.redirectTo({
            url: '../step2/index?typeId='+typeId.productTypeId
        })

    },
    bindChange: function(e) {
        const val = e.detail.value
        var bigType = this.data.bigTypes[val[0]];
        var letType = this.data.letTypes[val[1]];

        if(val[0] == valueBak[0]){
            this.setData({
                letTypeName : letType.productTypeName
            })
            typeId = letType;
        }else{
            typeId = bigType;
            this.queryLetProductTypes(bigType.productTypeId,[]);
            this.setData({
                bigTypeName : bigType.productTypeName,
                value:[val[0],0]
            })
        }

        valueBak = val;
    },
    queryLetProductTypes:function (parentId,exceptIds) {
        wx.showLoading({
            title: '数据加载..',
            icon: 'loading',
            duration: config.TIME_OUT,
            mask:true
        })
        var that=this;
        wx.request({
            url: config.COMMPT_REQPATH,
            header: {
                "Content-Type": "application/x-www-form-urlencoded"
            },
            method:'POST',
            data: {
                accessToken: '',
                refreshToken: '',
                serviceName: 'dmService',
                methodName: 'selectProductsByParentIdExceptIds',
                reqData:JSON.stringify({
                    parentId:parentId,
                    exceptId:exceptIds
                })
            },
            success: function (res) {
                var resultData = res.data;
                var products  = resultData.data.products;
                if(products!= null && products.length>0) {
                    that.setData({
                        letTypes: products,
                        letTypeName: products[0].productTypeName
                    })
                    typeId = products[0];
                }else {
                    that.setData({
                        letTypes: [],
                        letTypeName: ''
                    })
                }
            },
            complete:function () {
                wx.hideLoading();
            }
        })
    }
})